/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.data.osmt;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import android.graphics.drawable.Drawable;
import de.fuberlin.kosmik.StrUtils;

/**
 * Data Container for OsmtFeature.
 * 
 * @author Johannes
 * 
 * @see OsmtFeature
 * @see OsmtBundle
 */
public class OsmtFeatureRecord extends OsmtElementRecord {

	/** The key is part of the PK. */
	public String osmKey;
	/** The value is part of the PK. */
	public String osmValue;
	/** Contain a link to a wiki page. */
	public String wiki;
	/** Refer to a image that shows how this feature would be rendered. */
	public String rendering;
	/** Contain a photo of the mapped object in reality. */
	public String photo;
	/** specify if this feature is modifiable in combination with a bundle. */
	public boolean modifiable = true;

	@Override
	public String toString() {
		return String.format("%s = %s", osmKey, osmValue);
	}

	private static Drawable downloadImage(String urlString) throws IOException {
		URL url = new URL(urlString);
		Object content = url.getContent();
		InputStream inputStream = (InputStream) content;
		return Drawable.createFromStream(inputStream, "src");
	}

	private static Drawable downloadFeatureImageWithName(String imageName) {
		final String wikiPhotoAddress = String.format(
				"http://wiki.openstreetmap.org/w/images/thumb/1/10/%s/100px-%s", imageName,
				imageName);
		boolean downloadSucceed;
		try {
			return downloadImage(wikiPhotoAddress);
		} catch (IOException e) {
			downloadSucceed = false;
		}
		if (!downloadSucceed) {
			// try alternative address
			final String uploadPhotoAddress = String.format(
					"http://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/%s/100px-%s",
					imageName, imageName);
			try {
				return downloadImage(uploadPhotoAddress);
			} catch (IOException e) {
				return null;
			}
		}
		return null;
	}

	/**
	 * Downloads an image of a photo from the wiki.
	 * 
	 * @param featureRecord The record contains the name of the photo.
	 * @return a Drawable, if the image could be downloaded. Otherwise null.
	 */
	public static Drawable downloadFeaturePhoto(OsmtFeatureRecord featureRecord) {
		if (featureRecord != null && StrUtils.isNotEmpty(featureRecord.photo)) {
			return downloadFeatureImageWithName(featureRecord.photo);
		}
		return null;
	}

	/**
	 * Downloads an rendering image from the wiki.
	 * 
	 * @param featureRecord The record contains the name of the render image.
	 * @return a Drawable, if the image could be downloaded. Otherwise null.
	 */
	public static Drawable downloadFeatureRenderImage(OsmtFeatureRecord featureRecord) {
		if (featureRecord != null && StrUtils.isNotEmpty(featureRecord.rendering)) {
			return downloadFeatureImageWithName(featureRecord.rendering);
		}
		return null;
	}
}
